package demo1;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectClassDemo {
    //创建反射对象
    public static void reflectNewInstance() {
        Class<?> classStudent = null;
        try {
            //拿到Class对象
            classStudent = Class.forName("demo1.Student");
            //创建类的实例
            Student student = (Student) classStudent.newInstance();
            System.out.println("获得学生对象：" + student);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }


    //反射私有的构造方法
    public static void reflectPrivateConstructor() {
        Class<?> classStudent = null;
        try {
            //拿到Class对象
            classStudent = Class.forName("demo1.Student");
            //获得私有的构造方法，传入对应的参数
            Constructor<?> constructor = classStudent.getDeclaredConstructor(String.class, int.class);
            //获得公有的构造方法
            //Constructor<?> constructor = classStudent.getConstructor();

            //设置为true后可修改访问权限，访问私有的构造方法。相当于经过确认的。
            constructor.setAccessible(true);

            //创建类的实例，调用两个参数的构造方法
            Student student = (Student) constructor.newInstance("xiaoming",15);
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    //反射私有属性
    public static void reflectPrivateField() {
        Class<?> classReflect = null;
        try {
            //拿到Class对象
            classReflect = Class.forName("demo1.Student");
            //获得私有的属性，传入对应的属性名
            Field field = classReflect.getDeclaredField("name");
            //确认可以调用私有属性的语句
            field.setAccessible(true);
            //创建类的实例
            Student student = (Student) classReflect.newInstance();

            //修改私有属性
            field.set(student,"奶酪");
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    //反射私有方法
    public static void reflectPrivateMethod() {
        Class<?> classReflect = null;
        try {
            //拿到Class对象
            classReflect = Class.forName("demo1.Student");
            //获得私有的方法，传入对应的属性名
            Method method = classReflect.getDeclaredMethod("function", String.class);
            //确认可以调用私有方法的语句
            method.setAccessible(true);
            //创建类的实例
            Student student = (Student) classReflect.newInstance();

            //给私有方法传参
            method.invoke(student,"我是一个反射的参数");

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        //reflectNewInstance();
        //reflectPrivateConstructor();
        //reflectPrivateField();
        reflectPrivateMethod();
    }
}
